package arithmetic.functionTest.myStream;

import java.util.Objects;
import java.util.function.Consumer;

/**
 *
 * 简化版Sink,
 * @see java.util.stream.Sink
 * 就是继承Sonsumer,并开始消费前 调用begin,结束消费调用end方法
 *
 * 无装箱拆箱优化,无取消操作
 * @author jiangfeng 2020/4/26 17:08
 */
public interface MySink<T> extends Consumer<T> {

    /**
     * 开始
     * @param size
     */
    default void begin(long size) {}

    /**
     * 结束
     */
    default void end() {}


    /**
     * 实现Sink链
     * @param <T>
     * @param <E_OUT>
     */
    static abstract class ChainedReference<T, E_OUT> implements MySink<T> {

        protected final MySink<? super E_OUT> downstream;

        public ChainedReference(MySink<? super E_OUT> downstream) {
            this.downstream = Objects.requireNonNull(downstream);
        }

        @Override
        public void begin(long size) {
            downstream.begin(size);
        }

        @Override
        public void end() {
            downstream.end();
        }

    }
}
